<template>
  <div >
    <!-- 添加合同项目 -->
    <el-dialog title="信息" :visible.sync="openAddContractProgram" width="900px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
        <el-form ref="formParams" :model="formParams" :rules="rules" label-width="150px" :inline="true">
          <el-row>
            <el-col :span="4">
              <span class="pull-right font-weight"><span class="pr-5 text-danger">*</span>选择仓库</span>
            </el-col>
            <el-col :span="20">
              <el-scrollbar style="height:180px;width:700px; margin:0 10px;" class="mb20">
                <el-form-item>
                  <el-checkbox v-for="item in selectWarehouseList" :key="'warehouse-' + item.spckid" v-model="item.selected" :label="item.spckmc" :true-label="item.spckid" false-label=""></el-checkbox>
                </el-form-item>
              </el-scrollbar>
            </el-col>
          </el-row>
          
          <el-row class="mt20">
            <el-col :span="12">
              <el-form-item label="开始日期" prop="sdate">
                <el-date-picker
                  v-model="formParams.sdate"
                  value-format="yyyy-MM-dd"
                  type="date"
                  :disabled="true"
                  >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="结束日期" prop="edate">
                  <el-date-picker
                    v-model="formParams.edate"
                    value-format="yyyy-MM-dd"
                    type="date"
                    :disabled="true"
                    >
                  </el-date-picker>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="仓储费(吨/天/元)" prop="csdj01" :rules="rules.csdj01">
                <el-input
                v-model="formParams.csdj01"
                placeholder="请输入仓储费(吨/天/元)"
                clearable
                type="number"
                min="0"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="货权转移费用(吨/元)" prop="csdj04" :rules="rules.csdj04">
                <el-input
                v-model="formParams.csdj04"
                placeholder="请输入货权转移费用(吨/元)"
                clearable
                type="number"
                min="0"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="出仓装卸费(吨/元)" prop="csdj02" :rules="rules.csdj02">
                <el-input
                v-model="formParams.csdj02"
                placeholder="请输入出仓装卸费(吨/元)"
                clearable
                type="number"
                min="0"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="出仓不足吨数" prop="csdj02t" >
                <el-input
                v-model="formParams.csdj02t"
                placeholder="请输入出仓不足吨数"
                clearable
                type="number"
                min="0"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="入仓装卸费(吨/元)" prop="csdj03" :rules="rules.csdj03">
                <el-input
                v-model="formParams.csdj03"
                placeholder="请输入入仓装卸费(吨/元)"
                clearable
                type="number"
                min="0"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="入仓不足吨数" prop="csdj03t">
                <el-input
                v-model="formParams.csdj03t"
                placeholder="请输入入仓不足吨数"
                clearable
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="备注" prop="csbz">
                <el-input
                    v-model="formParams.csbz"
                    placeholder="请输入备注"
                    clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="合同编号" prop="csbh">
                    <el-input
                    v-model="formParams.csbh"
                    :disabled="true"
                    />
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="版次" prop="cx">
                <el-input
                v-model="formParams.cx"
                :disabled="true"
                />
            </el-form-item>
            </el-col>
        </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleAdd('formParams')" v-hasPermi="['zhcloud:sfbz:add']">添加合同项目</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { addContractProgram } from "@/api/storage/contractFeeStandard"
import { selectWarehouse22  } from "@/api/storage/warehouse"
import { WEIGHT_DIGIT, AMOUNT_DIGIT } from "@/utils/constant"
import { getDigit } from "@/utils/amount"

export default {
  name: "ContractProgramAdd",
  data() {
    return {
      //仓库加载
      selectWarehouse22Loading: true,
       //表单校验
      rules: {
        csdj01: [
          { required: true, message: "仓储费(吨/天/元)不能为空", trigger: "blur" },
        ],
         csdj04: [
          { required: true, message: "货权转移费用(吨/元)不能为空", trigger: "blur" },
        ],
         csdj02: [
          { required: true, message: "出仓装卸费(吨/元)不能为空", trigger: "blur" },
        ],
        csdj03: [
          { required: true, message: "入仓装卸费(吨/元)不能为空", trigger: "blur" },
        ],
        csdj02t: [
          { required: true, message: "出仓不足吨数不能为空", trigger: "blur" },
        ],
        csdj03t: [
          { required: true, message: "入仓不足吨数不能为空", trigger: "blur" },
        ],
      },
      //弹窗状态
      openAddContractProgram: false,
      formParams: {},
      //表单数据
      submitParams:{
        csbh: "",
        csbz: "",
        csdj01: "",
        csdj02: "",
        csdj02t: 0,
        csdj03: "",
        csdj03t: 0,
        csdj04: "",
        csuid: "",
        cx: "",
        edate: "",
        khmcid: "",
        sdate: "",
        spa: "",
        spc: ""
      },
      //仓库列表
      selectWarehouseList:[],
    };
  },
  watch:{
    'formParams.csdj01':function(data) {
      this.formParams.csdj01 = getDigit(data, AMOUNT_DIGIT)
    },
    'formParams.csdj04':function(data) {
      this.formParams.csdj04 = getDigit(data, AMOUNT_DIGIT)
    },
    'formParams.csdj03':function(data) {
      this.formParams.csdj03 = getDigit(data, AMOUNT_DIGIT)
    },
    'formParams.csdj02':function(data) {
      this.formParams.csdj02 = getDigit(data, AMOUNT_DIGIT)
    },
    'formParams.csdj02t':function(data) {
      this.formParams.csdj02t = getDigit(data, WEIGHT_DIGIT)
    },
    'formParams.csdj03t':function(data) {
      this.formParams.csdj03t = getDigit(data, WEIGHT_DIGIT)
    },
  },
  methods: {
      /**获取仓库 */
    stockSelect() {
      this.selectWarehouse22Loading=true
      selectWarehouse22({}).then(response => {
        this.selectWarehouseList = response.data;
        this.selectWarehouse22Loading=false
      });
    },
    /**添加合同项目 */
    handleAdd(formName) {
      this.$refs[formName].validate((valid, data) => {
        if(valid) {
            this.$modal.confirm("是否添加主要合同明细?").then(() => {
                const warehouseArr = this.selectWarehouseList
                const submitParams = this.submitParams
                const formParams = this.formParams
                let warehouseStr = ""
                for(let i in warehouseArr) {
                  if(warehouseArr[i].selected) {
                    warehouseStr += warehouseArr[i].selected + ","
                  }
                }
                warehouseStr = warehouseStr.substring(0, warehouseStr.length - 1)
                if(!warehouseStr) return this.$modal.msgError("库存不能为空，请选择库存")
                this.formParams.spc = warehouseStr
                for(let i in submitParams) {
                  submitParams[i] = formParams[i]
                  submitParams[i] = submitParams[i] == undefined?'':submitParams[i]
                }
                this.$modal.loading("数据正在添加中，请稍等...")
                this.submitParams = submitParams
                addContractProgram(this.submitParams).then(response => {
                    this.$modal.closeLoading()
                    this.$modal.msgSuccess('添加成功！')
                    this.close()
                    this.$emit('refresh', true)
                }).catch(()=> {
                    this.$modal.closeLoading()
                })
            }).catch(() => {})
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭窗口 */
    close() {
      this.changeAddContractProgram()
      this.reset()
    },
    /**改变窗口状态 */
    changeAddContractProgram() {
      this.openAddContractProgram = !this.openAddContractProgram;
    },

    /**打开弹窗 */
    open(data) {
      this.changeAddContractProgram()
      this.formParams = {
        ...this.formParams,
        csuid:data.uid,
        cx:data.cx,
        sdate:data.sdate,
        edate:data.edate,
        khmcid:data.khid,
        csbh:data.csbh
      }
      this.stockSelect()
    },
  }
};
</script>
